package com.bw.lib_storage.db.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.bw.lib_storage.db.entity.History

@Dao
interface HistoryDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(history: History)

    @Insert
    fun insertAll(vararg history:History)


    @Delete
    fun deleteHistory(history: History)

    @Update
    fun updateHistory(history: History)

    @Query("SELECT * FROM history")
    fun getAllHistory():List<History>

    @Query("SELECT * FROM history WHERE title LIKE '%'+:title+'%'")
    fun getHistoryByTitle(title:String):List<History>
    
    @Query("SELECT * FROM history WHERE content = :content")
    fun getHistoryByContent(content: String): History?
    
    @Query("UPDATE history SET view_count = view_count + 1, query_time = :newTime WHERE content = :content")
    fun incrementViewCount(content: String, newTime: Long)
    
    @Query("DELETE FROM history WHERE id NOT IN (SELECT MIN(id) FROM history GROUP BY content)")
    fun deleteDuplicateHistories()
}